import request from '@/utils/request'
import apiConfig from '@/api/config'
import { useUserStore } from '@/pinia/modules/user'
import FileSaver from 'file-saver'
// import qs from 'qs'

// 登陆
export const login = data => {
  return request({
    url: `${apiConfig.baseApi}/auths/login`,
    method: 'post',
    // headers: {
    //   'Content-Type': 'application/x-www-form-urlencoded'
    // },
    data
  })
}

// 获取用户密码规范信息
export const getUserCheckPwd = () => {
  return request({
    url: `${apiConfig.baseApi}/auths/checkPwd`,
    method: 'get'
  })
}

export const get_student_id = () => {
  return request({
    url: `${apiConfig.trainingApi}/students/getStudent`,
    method: 'get'
  })
}

// 获取下一级区划
export const get_area_list = () => {
  return request({
    url: `${apiConfig.baseApi}/area/getNextAreaInfoByCode`,
    method: 'post'
  })
}

// 获取验证码
export const getCaptcha = params => {
  return request({
    url: `${apiConfig.baseApi}/auths/captcha`,
    method: 'get',
    params
  })
}

// 获取字典
export const getDictTree = params => {
  return request({
    url: `${apiConfig.baseApi}/dicts/tree`,
    method: 'get',
    params
  })
}

// 获取地区区划
export const gerAreaByCode = data => {
  return request({
    url: `${apiConfig.baseApi}/areas/next-children/${data}`,
    method: 'get'
  })
}

// 获取地区区划
export const getAreaInfo = data => {
  return request({
    url: `${apiConfig.baseApi}/areas/${data}`,
    method: 'get'
  })
}

// 退出登陆
export const loginOut = () => {
  return request({
    url: `${apiConfig.baseApi}/auths/logout`,
    method: 'get'
  })
}

// 导出
export const downLoad = (params, url) => {
  return request({
    url: `${apiConfig.businessApi}${url}`,
    method: 'get',
    params,
    responseType: 'blob'
  })
}

// 下载文件
export const downLoadFile = async (ids, fileName) => {
  const blob = await request({
    url: `${useUserStore().fileUrl}/batch_download?ids=${ids}&package_name=${fileName}`,
    method: 'get',
    responseType: 'blob'
  })
  FileSaver.saveAs(blob, `${fileName}.zip`)
}

// 上传
export const upload_file = data => {
  return request({
    method: 'post',
    url: `${apiConfig.baseApi}/files`,
    data: data
  })
}

// 回显附件
export const get_files = data => {
  return request({
    method: 'get',
    url: `/oss-server/v1/download_base64/${data}`,
    data
  })
}

// 基础参数获取文件
export const getSysparamFileServer = () => {
  return request({
    url: `${apiConfig.baseApi}/auths/sys-param`,
    method: 'get'
  })
}

// 信息查询列表
export const get_list = (params, listApiUrl) => {
  return request({
    url: `${apiConfig.businessApi}${listApiUrl}`,
    method: 'get',
    params
  })
}

// 信息查询详情
export const get_detail = (id,detailApiUrl) => {
  return request({
    url: `${apiConfig.businessApi}${detailApiUrl}/${id}`,
    method: 'get'
  })
}

// 获取地图点
export const get_map_item = params => {
  return request({
    url: `${apiConfig.businessApi}/gov-portal/org-map`,
    // url: `${apiConfig.businessApi}/gov-portal/org`,
    method: 'get',
    params
  })
}

// 获取地图机构详情
export const get_org_detail = (id) => {
  return request({
    url: `${apiConfig.businessApi}/gov-portal/org/${id}`,
    method: 'get'
  })
}
